package org.lgiavedoni.android.franq.service.phone.calendar;

import java.util.List;

import lombok.Getter;

import org.lgiavedoni.android.franq.core.calendar.Calendar;
import org.lgiavedoni.android.franq.core.calendar.CalendarEvent;
import org.lgiavedoni.android.franq.core.service.Service;
import org.lgiavedoni.android.franq.service.config.ConfigService;
import org.lgiavedoni.android.franq.service.config.ConfigService.ConfigKey;
import org.lgiavedoni.android.franq.service.phone.calendar.dao.CalendarDAO;
import org.lgiavedoni.android.franq.util.string.StringUtil;

public class CalendarService extends Service {

	public static final CalendarService inst = new CalendarService();

	private CalendarDAO calendarDAO;
	@Getter(lazy = true)
	private final Calendar myCalendar = createCalendar();

	public List<Calendar> getCalendars() {
		return calendarDAO.getAllCalendars();
	}

	public List<CalendarEvent> getAllCalendarEvents() {
		return calendarDAO.getAllCalendarEntryes(getMyCalendar());
	}

	public void removeAllCalendarEvents() {
		removeAllCalendarEvents(getMyCalendar()); 
	}

	public void removeAllCalendarEvents(Calendar calendar) {
		for (CalendarEvent ce : getAllCalendarEvents(calendar))
			removeCalendarEvent(ce);
	}

	public List<CalendarEvent> getAllCalendarEvents(Calendar calendar) {
		return calendarDAO.getAllCalendarEntryes(calendar);
	}

	public List<CalendarEvent> getFutureCalendarEvents() {
		return getFutureCalendarEvents(getMyCalendar());
	}

	public List<CalendarEvent> getFutureCalendarEvents(Calendar calendar) {
		return calendarDAO.getFutureCalendarEntryes(calendar);
	}

	public void addCalendarEvent(CalendarEvent ce) {
		addCalendarEvent(getMyCalendar(), ce);
	}

	public void addCalendarEvent(Calendar c, CalendarEvent ce) {
		if (isStarted())
			calendarDAO.addEvent(c, ce);
	}

	public void removeCalendarEvent(CalendarEvent ce) {
		if (isStarted())
			calendarDAO.removeEvent(ce);
	}

	@Override
	public String getServiceName() {
		return "calendar";
	}

	@Override
	protected void _run() {
	}

	@Override
	protected void stopInternal() {
		// myCalendar = null;
		calendarDAO = null;
	}

	private Calendar getCalendarByName(String name) {
		for (Calendar c : getCalendars()) {
			if (StringUtil.equals(name, c.getTitle()))
				return c;
		}
		return null;
	}

	public Calendar getOrCreateCalendar(String calendarName) {
		Calendar calendar = getCalendarByName(calendarName);
		if (calendar == null) {
			calendar = calendarDAO.createCalendar(calendarName);
		}
		return calendar;
	}

	private Calendar createCalendar() {
		Calendar calendar = getCalendarByName(ConfigService.inst.getValue(ConfigKey.CALENDAR_NAME));
		if (calendar == null) {
			calendar = calendarDAO.createCalendar(ConfigService.inst.getValue(ConfigKey.CALENDAR_NAME));
		}
		return calendar;
	}

	@Override
	protected void startInternal() {
		// Set calendarDAO
		calendarDAO = (CalendarDAO) getImplementation(CalendarDAO.class);
		// Set myCalendar
		// myCalendar= createCalendar();

	}
}
